import json
import time

import pycountry
import requests
from flask import Flask
from flask import render_template, jsonify, request
from flask_cors import CORS

from utils.db_select import *

app = Flask(__name__)
cors = CORS(app)


# TODO：人物信息卡片上需要有一个定位

@app.route('/')
def hello_world():  # put application's code here
    return render_template("index.html")


@app.route('/layout')
def layout():
    return render_template("layout.html")


@app.route("/page1")
def page1():
    return render_template("page1.html")


@app.route("/basic_data")
def get_basicData():
    basic_key = request.args.get('key')
    return jsonify(get_basic_data(basic_key))


@app.route("/sentiment")
def get_sentiment():
    try:
        beginDate = request.args.get('timeBegin')
        endDate = request.args.get('timeEnd')
        return jsonify(get_sentiment_statistic(beginDate, endDate))
    except:
        return jsonify(get_sentiment_statistic())


@app.route("/emotion")
def get_emotion():
    try:
        beginDate = request.args.get('timeBegin')
        endDate = request.args.get('timeEnd')
        return jsonify(get_emotion_statistic(beginDate, endDate))
    except:
        return jsonify(get_emotion_statistic())


@app.route("/blogs")
def get_blogs():
    pageSize = request.args.get('pageSize')
    pageNum = request.args.get('pageNum')
    orderBy = request.args.get('orderBy')
    return jsonify(get_blogs_data(pageSize, pageNum, orderBy))


@app.route("/event_trend")
def get_event():
    try:
        beginDate = request.args.get('timeBegin')
        endDate = request.args.get('timeEnd')
        if beginDate is None or endDate is None:
            return jsonify(get_event_data('2000/01/01', '3000/01/01'))
        elif beginDate is '' or endDate is '':
            return jsonify(get_event_data('2000/01/01', '3000/01/01'))
        else:
            return jsonify(get_event_data(beginDate, endDate))
    except:
        return jsonify(get_event_data('2000/01/01', '3000/01/01'))


@app.route("/blogs_date")
def get_blogs_with_date():
    pageSize = request.args.get('pageSize')
    pageNum = request.args.get('pageNum')
    orderBy = request.args.get('orderBy')
    date = request.args.get('date')
    return jsonify(get_data_with_date(pageSize, pageNum, orderBy, date))


@app.route("/RPC_world_map")
def get_world_date():
    dataAddr, locations = get_count_cnt()
    # with open('./static/resource/jsons/dataArr_chn.txt', 'r', encoding='utf-8') as reader:
    #     dataAddr = eval(reader.read())
    # with open('./static/resource/jsons/locations_val_coord_single.txt', 'r', encoding='utf-8') as reader:
    #     locations = eval(reader.read())
    # time.sleep(10)
    locations = [position for idx, position in enumerate(locations) if idx % 10 == 0]
    return jsonify({'dataAddr': dataAddr, 'locations': locations})


@app.route("/tci")
def get_tciData():
    with open("tci.json", "r") as f:
        data = json.load(f)
    # data = select_tci()
    # print(data)
    time.sleep(5)
    # print("获取意见领袖数据成功")
    return jsonify(data)


@app.route("/page2")
def page2():
    return render_template("page2.html")


@app.route("/query_tweet")
def get_tweet_key():
    key = str(request.args.get('tweetKey'))
    data = select_tweet(key)
    return jsonify(data)


@app.route("/page4")
def page4():
    print("加载page4")
    return render_template("page4.html")


@app.route('/data_wordcloud')
def get_data():
    with open("word.json", "r") as f:
        data = json.load(f)
        time.sleep(5)
        print("获取词云数据成功")
    return jsonify(data)


@app.route('/get_count_loc')
def getConLoc():
    position = request.args.get('region')
    return jsonify(get_region_location(position))


if __name__ == '__main__':
    # get_world_date()
    app.run(host="0.0.0.0", port=8000, debug=True)
